package services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataAccess.DeviceDA;
import DataAccess.NguoiDungDA;
import DataAccess.TinNhanDA;

import pojo.DevicePOJO;
import pojo.NguoiDungPOJO;
import pojo.PairValue;
import pojo.TinNhanPOJO;
import util.ClientSender;
import util.GCMSender;
import util.XMLBuidler;

/**
 * Servlet implementation class HeThong
 */
// @WebServlet("/HeThong")
@WebServlet(name = "HeThong", urlPatterns = { "/HeThong" })
public class HeThongServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @throws ServletException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		//super.doGet(request, response);
		PrintWriter out = response.getWriter();
		try {

			String cmd = getParameter(request, Protocol.CMD);

			if (cmd != null) {
				System.out.print("CMD=" + cmd);
				if (cmd.equals(Protocol.DANG_KY_DEVICE)) {
					String regID = getParameter(request, Protocol.REG_ID);
					String tenDangNhap = "phu";
					DeviceDA da = new DeviceDA();
					DevicePOJO dv = new DevicePOJO(regID, tenDangNhap);
					if (da.insert(dv)) {
						out.print(true);
						System.out.print(Protocol.DANG_KY_DEVICE + " regId= " + regID);
					} else {
						out.print(false);
					}
				} else if (cmd.equals(Protocol.DANG_NHAP)) {
					String tenDangNhap, matKhau, regID;
					tenDangNhap = getParameter(request, Protocol.TEN_DANG_NHAP);
					matKhau = getParameter(request, Protocol.MAT_KHAU);
					regID = getParameter(request, Protocol.REG_ID);

					NguoiDungDA da = new NguoiDungDA();
					NguoiDungPOJO nd = da.get(tenDangNhap);
					if (nd != null && nd.getMatKhau().equals(matKhau)) {
						da.updateDeiveID(tenDangNhap, regID);
						out.print(true);

					} else {
						out.print(false);
					}
				} else if (cmd.equals(Protocol.DANH_SACH_NHOM)) {
					String tenDangNhap = getParameter(request, Protocol.TEN_DANG_NHAP);
					String contact = XMLBuidler.getContact(tenDangNhap);
					out.print(contact);
					System.out.print(contact);
				} else if (cmd.equals(Protocol.LAY_TIN_NHAN)) {
					String tenDangNhap = getParameter(request, Protocol.TEN_DANG_NHAP);
					TinNhanDA da = new TinNhanDA();
					ArrayList<TinNhanPOJO> lstTinNhan = da.getByNguoiNhan(tenDangNhap, 1);
					out.print(XMLBuidler.layTinNhan(lstTinNhan));
				} else if (cmd.equals(Protocol.GUI_TIN_NHAN)) {
					String nguoiGui = getParameter(request, Protocol.NGUOI_GUI);
					String nguoiNhan = getParameter(request, Protocol.NGUOI_NHAN);
					String noiDung = getParameter(request, Protocol.NOI_DUNG);

					TinNhanPOJO tinNhan = new TinNhanPOJO();
					NguoiDungDA nguoiDungDA = new NguoiDungDA();
					NguoiDungPOJO nd = nguoiDungDA.get(nguoiGui);

					tinNhan.setIdNguoiGui(nd.getId());
					nd = nguoiDungDA.get(nguoiNhan);
					tinNhan.setIdNguoiNhan(nd.getId());
					tinNhan.setNoiDung(noiDung);

					TinNhanDA tinNhanDA = new TinNhanDA();
					int id = tinNhanDA.insert(tinNhan);
					tinNhan = tinNhanDA.get(id);
//					ArrayList<TinNhanPOJO> lstTinNhan = new ArrayList<TinNhanPOJO>();
//					lstTinNhan.add(tinNhan);

					ArrayList<PairValue> params = new ArrayList<PairValue>();
					params.add(new PairValue(Protocol.CMD, Protocol.TIN_NHAN_CHAT));
					params.add(new PairValue(Protocol.NGUOI_GUI, nguoiGui));
					params.add(new PairValue(Protocol.NOI_DUNG, noiDung));
					try {
						ClientSender.send(nd.getDeviceID(), params, 500);
						// ClientSender.send(nd.getDeviceID(), params);
						out.print("Da gui");
					} catch (Exception e) {
						out.print("Ko gui dc");
					}
				}
			}

		} catch (Exception e) {
			out.print("Lỗi: " + e.getMessage());
			System.out.print("Lỗi: " + e.getMessage());
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		doGet(request, response);
	}

}
